const form=document.querySelector('form')
const nameInp=document.querySelector('.username')
const pwdInp=document.querySelector('.password')
const err=document.querySelector('.error')

form.addEventListener('submit',e=>{
    e=e||window.event
    e.preventDefault()
    const name=nameInp.value
    const pwd=pwdInp.value
    if(!name||!pwd) return alert('请完整填写表单')
    ajax({
        url:'http://localhost:8888/users/login',
        method:'POST',
        data:`username=${name}&password=${pwd}`,
        dataType:'json',
        success(res){
            if(res.node===0){
            err.getElementsByClassName.visibility='visible'
            return
            }
           window.localStorage.setItem('id',res.user.id)
           window.localStorage.setItem('token',res.token)
           const url=window.localStorage.getItem('url')
           window.localStorage.removeItem('url')
           window.location.href=`./${url?url:'index'}.html`
        }
        
    })
})